const Router = require('@koa/router');
const menuController = require('../../controllers/menuController');
const { authenticate } = require('../../middleware/authMiddleware');
const { checkPermission } = require('../../middleware/permissionMiddleware');

const router = new Router({
    prefix: '/api/menus'
});

/**
 * @swagger
 * components:
 *   schemas:
 *     Menu:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *         title:
 *           type: string
 *         path:
 *           type: string
 *         icon:
 *           type: string
 *         parent_id:
 *           type: string
 *         level:
 *           type: number
 *         sort:
 *           type: number
 *         hidden:
 *           type: boolean
 */

/**
 * @swagger
 * /api/menus:
 *   get:
 *     summary: 获取菜单树
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页显示的数量
 *     responses:
 *       200:
 *         description: 成功获取菜单树
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 操作成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: number
 *                       description: 总记录数
 *                     page:
 *                       type: number
 *                       description: 当前页码
 *                     limit:
 *                       type: number
 *                       description: 每页显示数量
 *                     data:
 *                       type: array
 *                       description: 分页后的菜单树
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           title:
 *                             type: string
 *                           path:
 *                             type: string
 *                           icon:
 *                             type: string
 *                           meta:
 *                             type: object
 *                             properties:
 *                               title:
 *                                 type: string
 *                               icon:
 *                                 type: string
 *                               isShow:
 *                                 type: boolean
 *                           children:
 *                             type: array
 *                             items:
 *                               $ref: '#/components/schemas/Menu'
 *                     tree:
 *                       type: array
 *                       description: 完整的菜单树
 *                       items:
 *                         $ref: '#/components/schemas/Menu'
 *       500:
 *         description: 服务器错误
 */
router.get('/', authenticate, checkPermission('system:menu:list'), menuController.getMenus);

/**
 * @swagger
 * /api/menus/{id}:
 *   get:
 *     summary: 获取单个菜单
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 */
router.get('/:id', authenticate, menuController.getMenuById);

/**
 * @swagger
 * /api/menus:
 *   post:
 *     summary: 创建菜单（需要权限）
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - path
 *               - type
 *             properties:
 *               parent_id:
 *                 type: string
 *                 description: 父菜单ID，顶级菜单为空字符串
 *               title:
 *                 type: string
 *                 description: 菜单标题
 *               path:
 *                 type: string
 *                 description: 菜单路径
 *               icon:
 *                 type: string
 *                 description: 菜单图标
 *               type:
 *                 type: number
 *                 description: 菜单类型（0：目录，1：菜单，2：按钮）
 *                 enum: [0, 1, 2]
 *               permission:
 *                 type: string
 *                 description: 权限标识
 *               component:
 *                 type: string
 *                 description: 组件路径
 *               sort:
 *                 type: number
 *                 description: 排序号
 *               hidden:
 *                 type: boolean
 *                 description: 是否隐藏
 */
router.post('/', authenticate, checkPermission('system:menu:add'), menuController.createMenu);

/**
 * @swagger
 * /api/menus/{id}:
 *   put:
 *     summary: 更新菜单（需要权限）
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 菜单ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               parent_id:
 *                 type: string
 *                 description: 父菜单ID，顶级菜单为空字符串
 *               title:
 *                 type: string
 *                 description: 菜单标题
 *               path:
 *                 type: string
 *                 description: 菜单路径
 *               icon:
 *                 type: string
 *                 description: 菜单图标
 *               level:
 *                 type: number
 *                 description: 菜单层级，从0开始
 *               sort:
 *                 type: number
 *                 description: 排序号，数字越小越靠前
 *               authority:
 *                 type: string
 *                 description: 权限标识
 *               hidden:
 *                 type: boolean
 *                 description: 是否隐藏菜单
 *           example:
 *             title: "系统管理"
 *             path: "/system"
 *             icon: "setting"
 *             parent_id: ""
 *             sort: 0
 *             authority: "system:view"
 *             hidden: false
 *     responses:
 *       200:
 *         description: 菜单更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 操作成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                     title:
 *                       type: string
 *                     path:
 *                       type: string
 *                     icon:
 *                       type: string
 *                     parent_id:
 *                       type: string
 *                     level:
 *                       type: number
 *                     sort:
 *                       type: number
 *                     authority:
 *                       type: string
 *                     hidden:
 *                       type: boolean
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 菜单不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', authenticate, checkPermission('system:menu:edit'), menuController.updateMenu);

/**
 * @swagger
 * /api/menus/{id}:
 *   delete:
 *     summary: 删除菜单（需要权限）
 *     tags: [Menus]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 菜单ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 删除成功
 *                 data:
 *                   type: null
 *       400:
 *         description: 存在子菜单，无法删除
 *       404:
 *         description: 菜单不存在
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id', authenticate, checkPermission('system:menu:del'), menuController.deleteMenu);

module.exports = router; 